/*
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

package XOrules;

import java.util.*;
import java.lang.*;
import java.math.*;
import org.ini4j.Ini;
import java.io.File;
import java.io.IOException;
import common.*;

public class KA {
    public final static String KA_0 = "0";
    
    public String	name			  = null;
    public KA		parentKA		  = null;
    public KA		subKA			  = null;
    public String	state			  = null;
    public TreeMap<String, Vector<String>> states = null;
    private Object	exitValue		  = null;

    public KA(KA parentKA_) {
	parentKA = parentKA_;
	subKA    = null;
	state    = KA_0;
	states	 = new TreeMap<String, Vector<String>>();
	_loadActions("KA.ini");
    }

    public Request doRequest(Request req) {
	Request res = null;

	if (this.states.get(state).contains(req.type)){
	    res = _doRequest(req);
	}
	else if (subKA != null){
	    res = subKA.doRequest(req);
	}

	if (res == null){
            res = req;
	}

	return res;
    }

    private Request _doRequest(Request req) {
        return req;
    }

    public void setState(String state_, Object exitValue_) throws Exception {
	if (state == state_){
	    return;
	}

	if (!this.states.keySet().contains(state_)){
	    throw new Exception("KA " + name + " havn't state " + state_);
	}
		
	if (state_ == KA_0) {
	    exitValue = exitValue_;
	    if (parentKA != null){
		parentKA._stopSubKA();
	    }
	}
	state = state_;
		
	if (subKA != null) {
	    subKA.exit();
	    subKA = null;
        }
		
	System.out.println("KA " + name + "->" + state);
    }

    private void exit() {
    }

    public void startSubKA(String KFname_) {
	subKA = Evaluator.callMethod(this, KFname_, this);
	subKA.doState("start");
    }
    
    private void doState(Object start) {
    }

    public void _stopSubKA() {
	doState(subKA.exitValue);
	subKA = null;
    }

    private void _loadActions(String fname) throws IOException {
	Ini		inir = new Ini(new File(fname));
	String		tmp  = "";
	Vector<String>	tvec = null;

	for(String key : inir.get(name).keySet()){
	    tmp  = inir.get(name).get(key);
	    tvec = new Vector<String>();
	    
	    for(String arrItem : tmp.split(",")){
		tvec.add(arrItem);
	    }
	    
	    states.put(v, tvec);
	}
    }
}
